
/**
 * 购物车模型类
 * @version 1.0.0
 * useDltCartStore
 */
class DltCartModel {
    /**
     * 购物车数据
     */
    cartData = [];
    cacheName = 'dlt_cart';
    /**
     * 添加到购物车
     * @param {Object} item - 需要添加的项
     */
    addCart(item) {
        this.cartData.push(item);
        window.localStorage.setItem(this.cacheName, JSON.stringify(this.cartData));
        console.log('【DltCart】addToCart ', item);
    }
    /**
     * 获取购物车数据
     * @returns {Array} - 购物车数据
     */
    getCart() {
        if (this.cartData.length === 0) {
            this.cartData = JSON.parse(window.localStorage.getItem(this.cacheName) ?? '[]');
        }
        return this.cartData;
    }
    /**
     * 清空购物车
     */
    clearCart() {
        this.cartData = [];
        window.localStorage.removeItem(this.cacheName);
        console.log('【DltCart】clearCart');
    }
    

    /**
     * 计算购物车中奖号码并着色
     * @param {String} drawResult 
     * @param {Object} cartData 
     * @returns {Array}
     */
    calcCart(drawResult, cartData) {
        if (Object.keys(cartData).length == 0 || drawResult.trim().length == 0) {
            return [];
        }

        let arrTemp = [], resultArrFront = [], resultArrBack = [], list = [], rowArr = [], count = 0;
        arrTemp = drawResult.trim().split(' ');
        resultArrFront = arrTemp.slice(0, split_index);
        resultArrBack = arrTemp.slice(split_index);

        let i = 0, num = '';
        for (let n in cartData) {
            i = parseInt(n) - 1;
            arrTemp = cartData[n].trim().split(' ');
            rowArr = [];
            if (i < split_index) {
                num = resultArrFront[i];
                arrTemp.forEach(str => {
                    if (str == num) {
                        rowArr.push(`<strong class="text-red-400">${str}</strong>`);
                        count++;
                    } else {
                        rowArr.push(str);
                    }
                })
            } else {
                num = resultArrBack[i - split_index];
                arrTemp.forEach(str => {
                    if (str == num) {
                        rowArr.push(`<strong class="text-red-400">${str}</strong>`);
                        count++;
                    } else {
                        rowArr.push(str);
                    }
                })
            }
            list.push({
                text: rowArr.join(' '),
                count,
            })
        }
        return list;
    }
    /**
     * 合并购物车并计算购物车中奖号码并着色
     * @param {String} drawResult 
     * @param {Array} cartArr 
     * @returns {Array}
     */
    calcCartForMerge(drawResult, cartArr, isAll = false) {
        if (cartArr.length == 0 || cartArr[0].length == 0) {
            if (isAll) {
                return {
                    text: '',
                    count: 0,
                    drawalCount: 0
                };
            }
            return {
                frontText: '',
                backText: '',
                frontCount: 0,
                backCount: 0,
                count: 0,
                frontDrawalCount: 0,
                backDrawalCount: 0,
                drawalCount: 0
            };
        }
        if (!drawResult) {
            drawResult = '00 00 00 00 00 00 00';
        }
        let arrTemp = [], resultArrFront = [], resultArrBack = [], drawalCount = 0, rowArr = [];
        arrTemp = drawResult.trim().split(' ');
        resultArrFront = arrTemp.slice(0, split_index);
        resultArrBack = arrTemp.slice(split_index);

        if (isAll) {
            let zoneAll = [];
            for (let arrRow of cartArr) {
                zoneAll = zoneAll.concat(arrRow);
            }
            zoneAll = [...new Set(zoneAll)].sort();
            zoneAll.forEach(str => {
                if (arrTemp.includes(str)) {
                    rowArr.push(`<strong class="text-red-400">${str}</strong>`);
                    drawalCount++;
                } else {
                    rowArr.push(str);
                }
            })
            return {
                text: rowArr.join(' '),
                count: rowArr.length,
                drawalCount
            };
        }

        let zoneFront = [], zoneBack = [], lineArrBack = [], backDrawalCount = 0;
        cartArr.forEach((arrRow, i) => {
            if (i < split_index) {
                zoneFront = zoneFront.concat(arrRow);
            } else {
                zoneBack = zoneBack.concat(arrRow);
            }
        })
        zoneFront = [...new Set(zoneFront)].sort();
        zoneBack = [...new Set(zoneBack)].sort();
        zoneFront.forEach(str => {
            if (resultArrFront.includes(str)) {
                rowArr.push(`<strong class="text-red-400">${str}</strong>`);
                drawalCount++;
            } else {
                rowArr.push(str);
            }
        })
        zoneBack.forEach(str => {
            if (resultArrBack.includes(str)) {
                lineArrBack.push(`<strong class="text-red-400">${str}</strong>`);
                backDrawalCount++;
            } else {
                lineArrBack.push(str);
            }
        })

        return {
            frontText: rowArr.join(' '),
            backText: lineArrBack.join(' '),
            frontCount: rowArr.length,
            backCount: lineArrBack.length,
            count: rowArr.length + lineArrBack.length,
            frontDrawalCount: drawalCount,
            backDrawalCount,
            drawalCount: drawalCount + backDrawalCount
        };
    }
}

export default DltCartModel;